******************************************************
*                 REPLICATION FILE 
*    Improving Workers' Performance in Small Firms: 
*    A Randomized Experiment on Goal Setting in Ghana  
*       		by Cettolin, Cole, Dalton     
*                      REStat                
*******************************************************

*prepare the data set:
set matsize 11000
clear
set more off 
use "Data_complete.dta"

encode employee_id, generate(employee)
encode employer_id, generate(firm)
order employee firm, first
by employee_id, sort: gen n_employee = _n == 1
by firm, sort: gen n_firm = _n == 1
tabulate treatment, generate(t)
label variable t1 "Production"
label variable t2 "Goals"
label variable t3 "No Intervention"
label define treatmentlbl 2 "No Intervention" 1 "Goals" 0 "Production"
label values treatment treatmentlbl
label define goalslbl 0 "Production" 1 "Goals"  
label values goals goalslbl

winsor bowls_peeled, generate(bowls_peeled_w)  p(0.05)
winsor peeling_time, generate(peeling_time_w)  p(0.05)
generate productivity=bowls_peeled/peeling_time_w
winsor productivity, generate(productivity_w) p(0.05)
sort firm date_peeled
by firm date_peeled: egen n_workers=count(employee)
by firm date_peeled: egen n_bowls=sum(bowls_peeled)
by firm date_peeled: generate tfp=n_bowls/n_workers
winsor tfp, generate(tfp_w) p(0.05)

label variable bowls_peeled_w "Bowls peeled"
label variable goals "Goals"
label variable Post "Post"
rename Statusoftraining trained
replace Goals_peeled_bowls=. if Post==0

*The lines in the following section - DESCRIPTIVES - also need to be run to complete the preparation of the database. 

**************
*DESCRIPTIVES*
**************
*Overview of the number of observations at the different stages of the study
*FIRMS - Table A.4

/// 425 firms in dataset
*110 No Intervention
codebook firm if treatment==2
*105 Production
codebook firm if treatment==0 
*210 Goals
codebook firm if treatment==1


/// 422 left after dropping those not found at time of interview: 
codebook firm if not_found==1
*110 No Intervention
codebook firm if treatment==2 & not_found==0
*104 Production
codebook firm if treatment==0 & not_found==0
*208 Goals
codebook firm if treatment==1 & not_found==0
*Exclude observation of firms that were not found at the time of the intervention: 
drop if not_found==1


/// 312 firms invited to train
/// 296 actually trained (+1 in No Intervention): 16 firms were unreachable at the time of training
*some firms were trained although they do not appear as such. Correct:
codebook firm if not_found==0 & trained==0 & peeling_dummy==1
replace trained=1 if trained==0 & peeling_dummy==1
*Trained firms:
*1 No Intervention
codebook firm if treatment==2 & not_found==0 & trained==1
*98 Production
codebook firm if treatment==0 & not_found==0 & trained==1 
*198 Goals
codebook firm if treatment==1 & not_found==0 & trained==1

// 272 firms were trained and peeled during relevant period
sort firm
by firm: egen peeled_x=max(peeling_dummy)
by firm: generate peeled=1 if peeled_x==1
by firm: replace peeled=0 if peeled_x==0
drop peeled_x
*0 in No Intervention
codebook firm if not_found==0 & peeled==1 & treatment==2
*86 in Production
codebook firm if not_found==0 & peeled==1 & treatment==0
*186 in Goals
codebook firm if not_found==0 & peeled==1 & treatment==1

sort firm
by firm: generate trained_peeled=1 if trained==1 & peeled==1 
replace trained_peeled=0 if missing(trained_peeled)
codebook firm if trained_peeled==1 & not_found==0
codebook firm if trained_peeled==0 & not_found==0

*check which firms took part in post-intervention survey:
sort firm
egen rowmiss_firm_end=rowmiss(F2*)
generate miss_firm_end=1 if rowmiss_firm_end==341
drop rowmiss_firm_end
replace miss_firm_end=0 if missing(miss_firm_end) 

*firms who peeld and answered the post-intervention survey 
*0 in No Intervention
codebook firm if not_found==0 & miss_firm_end==0 & treatment==2 
*86 in Production
codebook firm if not_found==0 & peeled==1 & miss_firm_end==0 & goals==0
*186 in Goals
codebook firm if not_found==0 & peeled==1 & miss_firm_end==0 & goals==1


*WORKERS - Table A.5

*first exclude peelers working for multiple firms
preserve 
collapse bowls_peeled, by(employee firm treatment trained peeled)
duplicates report employee if trained==1  & peeled==1
duplicates tag employee, generate (multi_firm)
sort multi_firm employee firm
*these three peelers work for multiple firms
tab multi_firm
restore
*only A0101402Y2 changes treatment, so he will be dropped 
drop if employee_id=="A0101402Y2"

*how many employees answered the BL:
sort employee
egen rowmiss_worker_bl=rowmiss(W1*)
generate miss_worker_bl=1 if rowmiss_worker_bl==213
drop rowmiss_worker_bl
replace miss_worker_bl=0 if missing(miss_worker_bl) 
*843: 208 Production, 415 Goals, 220 Control
count if n_employee==1 & not_found==0 & miss_worker_bl==0
sort treatment
by treatment: count if n_employee==1 & not_found==0 & miss_worker_bl==0

*how many employees answered the BL and were neither trained, nor peeled: 
count if n_employee==1 & not_found==0 & miss_worker_bl==0 & trained==0 & peeling_dummy==0
sort treatment
by treatment: count if n_employee==1 & not_found==0 & miss_worker_bl==0 & trained==0 & peeling_dummy==0

*how many employees were trained
*788: 267 Production, 521 Goals
count if n_employee==1 & not_found==0 & trained==1 & treatment!=2
by treatment: count if n_employee==1 & not_found==0 & trained==1 & treatment!=2

*how many employees were trained and were in BL:
*590: 196 in Production, 394 in Goals
count if n_employee==1 & not_found==0 & trained==1 & miss_worker_bl==0 & treatment!=2
by treatment: count if n_employee==1 & not_found==0 & trained==1 & miss_worker_bl==0

*how many employees were trained and were NOT in BL:
*198: 71 in Production, 127 in Goals
count if n_employee==1 & not_found==0 & trained==1 & miss_worker_bl==1 & treatment!=2
by treatment: count if n_employee==1 & not_found==0 & trained==1 & miss_worker_bl==1

*how many employees were trained and peeled:
*671: 221 Production, 450 Goals 
count if n_employee==1 & not_found==0 & trained==1 & peeling_dummy==1 & treatment!=2
by treatment: count if n_employee==1 & not_found==0 & trained==1 & peeling_dummy==1 

*how many employees were trained and peeled and were in BL:
*472: 149 Production, 323 Goals
count if n_employee==1 & not_found==0 & trained==1 & peeling_dummy==1 & miss_worker_bl==0
by treatment: count if n_employee==1 & not_found==0 & trained==1 & peeling_dummy==1 & miss_worker_bl==0

*how many employees were trained and peeled and were NOT in BL:
*198: 71 Production, 127 Goals
count if n_employee==1 & not_found==0 & trained==1 & peeling_dummy==1 & miss_worker_bl==1
by treatment: count if n_employee==1 & not_found==0 & trained==1 & peeling_dummy==1 & miss_worker_bl==1

*how many workers answered END
egen rowmiss_worker_end=rowmiss(W2*)
generate miss_worker_end=1 if rowmiss_worker_end==272
drop rowmiss_worker_end
replace miss_worker_end=0 if missing(miss_worker_end) 
*833: 206 in Production, 408 in Goals, 219 in Control
count if n_employee==1 & not_found==0 & miss_worker_end==0
by treatment: count if n_employee==1 & not_found==0 & miss_worker_end==0
*how many workers answered END who were trained and peeled
*469: 147 in Production, 322 in Goals
count if n_employee==1 & not_found==0 & trained==1 & peeling_dummy==1 & miss_worker_end==0
by treatment: count if n_employee==1 & not_found==0 & trained==1 & peeling_dummy==1 & miss_worker_end==0


***********
*ATTRITION*
***********

*Firms attrition - Table 1
preserve
set more off
collapse trained peeled F1_age F1_staff_total F1_staff_fam F1_firm_years_managing, by(firm treatment) 
*firms that got trained
reg trained treatment if treatment!=2, robust
reg trained treatment F1_age F1_staff_total F1_firm_years_managing  if treatment!=2, robust
*firms that got trained and peeled cassava
keep if trained==1
reg peeled treatment if treatment!=2, robust
reg peeled treatment F1_age F1_staff_total  F1_firm_years_managing if  treatment!=2, robust
restore
*create a variable counting days in which a firm is active
by firm date_peeled, sort: gen active_days= _n== 1
replace active_days=. if missing(date_peeled)
preserve
collapse F1_age F1_staff_total F1_staff_fam F1_firm_years_managing (sum) active_days, by(firm goals week)
*number of days in which firms are observed peeling
reg active_days goals i.week, robust
reg active_days goals F1_age F1_staff_total F1_firm_years_managing i.week, robust
restore

*Workers attrition - Table 2
*attrition between pre-intervention survey and training
generate compliant_ac=1 if trained==1 & miss_worker_bl==0
replace compliant_ac=0  if trained==0 & miss_worker_bl==0
tab compliant_ac if n_employee==1 & treatment!=2
preserve
set more off
keep if treatment!=2
collapse compliant_ac W1_age W1_hist_years piecerate flatrate, by(employee firm treatment) 
reg compliant_ac treatment, cluster(firm)
reg compliant_ac treatment W1_age W1_hist_years piecerate flatrate, cluster(firm)
restore
*attrition between pre-survey+trained+peeled and pre-survey+trained
generate compliant_cf=1 if peeling_dummy==1 & trained==1 & miss_worker_bl==0
replace compliant_cf=0  if peeling_dummy==0 & trained==1 & miss_worker_bl==0
tab compliant_cf if n_employee==1 & treatment!=2
preserve
set more off
keep if treatment!=2
collapse compliant_cf W1_age W1_hist_years piecerate flatrate, by(employee firm  treatment) 
reg compliant_cf treatment, cluster(firm)
reg compliant_cf treatment W1_age W1_hist_years piecerate flatrate, cluster(firm)
restore

****************
*BALANCE TABLES*
****************

*FIRMS - Table A.1
preserve
collapse F1_gender F1_age F1_educ F1_firm_years_managing F1_firm_days_per_wk F1_staff_total F1_staff_fam F1_sales_total_ex F1_sales_profit_ex F1_satis_life F1_firm_separate_accounts F1_firm_written_records F1_firm_documents_production F1_goal_set, by(employer_id treatment) 

orth_out F1_gender F1_age F1_educ F1_firm_years_managing F1_firm_days_per_wk F1_staff_total F1_staff_fam F1_sales_total_ex F1_sales_profit_ex F1_satis_life F1_firm_separate_accounts F1_firm_written_records F1_firm_documents_production F1_goal_set ///
using "TableBalanceFirm", by(treatment) se pcompare test  overall  vcount stars latex full replace
restore

*WORKERS - Table A.2
generate only_activity=1 if W1_hist_oth_act==2
replace only_activity=0 if  W1_hist_oth_act!=2 &  W1_hist_oth_act!=.
preserve
collapse W1_gender W1_age W1_educ W1_hist_years W1_inc piecerate flatrate only_activity W1_goal_job, by(employee_id treatment) 
orth_out W1_gender W1_age W1_educ W1_hist_years W1_inc piecerate flatrate only_activity W1_goal_job ///
using "TableBalanceWorkers", by(treatment) se pcompare test  overall  vcount stars latex full replace
restore


***********************
*GOAL SETTING BEHAVIOR*
***********************

*goal setting before the intervention - Table A.3
*workers
replace W1_goal_job=0 if W1_goal_dummy==0
reg W1_goal_job goals W1_age W1_gender W1_educ W1_hist_years piecerate if n_employee==1 & goals!=.
*outreg2 using "Tables/TableGoals_Char", tex replace addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
*firms
reg F1_goal_set goals F1_age F1_gender F1_educ F1_staff_total F1_staff_fam if n_firm==1 & goals!=.
*outreg2 using "Tables/TableGoals_Char", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label


*summary stats of chosen goals - Table A.7
generate gap_goals=Goals_peeled_bowls-bowls_peeled
preserve
keep if goals==1
keep if Post==1
summarize Goals_peeled_bowls
summarize bowls_peeled
summarize gap_goals 
restore

*correlates of goal setting behavior - Table A.8
preserve
keep if goals==1
keep if Post==1
local worker W1_age W1_gender W1_educ W1_hist_years
local employer F1_age F1_gender F1_educ  F1_staff_total F1_staff_fam
local incentives piecerate flatrate
collapse (mean) bowls_peeled_w Goals_peeled_bowls gap_goals `worker' `employer' `incentives', by(date_peeled employee firm week) 
sort employee date_peeled
generate bowls_peeled_before=bowls_peeled_w[_n-1]
generate goals_before=Goals_peeled_bowls[_n-1]
xtset employee date_peeled
xtreg Goals_peeled_bowls bowls_peeled_before goals_before week, cluster(firm) nonest
*outreg2 using "Table_goals", tex replace ctitle(Daily Goal) addtext(Clustered S.E, Firm) label dec(3) pdec(3)
xtreg Goals_peeled_bowls bowls_peeled_before goals_before  `incentives' `worker' week, cluster(firm) nonest
*outreg2 using "Table_goals", tex append ctitle(Daily Goal) addtext(Clustered S.E, Firm) label dec(3) pdec(3)
xtreg Goals_peeled_bowls bowls_peeled_before goals_before `incentives' `worker'  `employer' week, cluster(firm) nonest
*outreg2 using "Table_goals", tex append ctitle(Daily Goal) addtext(Clustered S.E, Firm) label dec(3) pdec(3)
xtreg gap_goals bowls_peeled_before goals_before week, cluster(firm) nonest
*outreg2 using "Table_goals", tex append ctitle(Daily Goal) addtext(Clustered S.E, Firm) label dec(3) pdec(3)
xtreg gap_goals bowls_peeled_before goals_before `incentives' `worker' week, cluster(firm) nonest
*outreg2 using "Table_goals", tex  append ctitle(Goal-Output) addtext(Clustered S.E, Firm) label dec(3) pdec(3)
xtreg gap_goals bowls_peeled_before goals_before `incentives' `worker'  `employer' week, cluster(firm) nonest
*outreg2 using "Table_goals", tex append ctitle(Goal-Output) addtext(Clustered S.E, Firm) label dec(3) pdec(3)
restore

*generate goal setting types
*classifcation based on averages - it is used only if there is a tie with that based on gaps (see below)
sort employee_id
by employee_id: generate achieve=(mean_gap==0)
replace achieve=. if mean_gap==.
by employee_id: generate under_achieve=(mean_gap>0)
replace under_achieve=. if mean_gap==.
by employee_id: generate over_achieve=(mean_gap<0)
replace over_achieve=. if mean_gap==.
by employee_id: generate goal_type=0 if under_achieve==1
replace goal_type=1 if achieve==1
replace goal_type=2 if over_achieve==1

*classification based on frequency of gaps, reported on page 22
sort employee
by employee: egen count_pos=count(gap_goals) if gap_goals>0 & Post==1
by employee: egen count_eq=count(gap_goals) if gap_goals==0 & Post==1
by employee: egen count_neg=count(gap_goals) if gap_goals<0 & Post==1
egen max_count=rowmax(count_pos count_eq count_neg)
by employee: egen most_frequent=max(max_count)
by employee: generate type=0 if most_frequent==count_pos
replace type=1 if most_frequent==count_eq
replace type=2 if most_frequent==count_neg
replace type=goal_type if count_pos==count_eq
replace type=goal_type if count_pos==count_neg
replace type=goal_type if count_eq==count_neg
label define typelabel 0 "under achiever" 1 "achiever" 2 "over achiever"
label values type typelabel
preserve
keep if goals==1
collapse type, by(employee_id)
tab type
restore


*performance by worker type - Figure A.1 (each of the three panels in the figure needs to be generated sequentially)
preserve
*collapse (max) max_count (max) goal_type count_pos count_eq count_neg bowls_peeled peeling_time, by(employee Post)
collapse (mean) bowls_peeled_w= bowls_peeled_w (sd) sdbowls =bowls_peeled_w  (count) n=bowls_peeled_w , by(Post type)
*collapse (mean) peeling_time_w = peeling_time_w (sd) sdtime =peeling_time_w (count) n=peeling_time , by(Post type)
*collapse (mean) productivity_w = productivity_w (sd) sdprod =productivity_w (count) n=productivity_w , by(Post type)
drop if type==.
generate hibowls= bowls_peeled_w + invttail(n-1,0.025)*(sdbowls  / sqrt(n))
generate lobowls  = bowls_peeled_w  - invttail(n-1,0.025)*(sdbowls  / sqrt(n))
*generate hitime= peeling_time_w+ invttail(n-1,0.025)*(sdtime  / sqrt(n))
*generate lotime  = peeling_time_w  - invttail(n-1,0.025)*(sdtime  / sqrt(n))
*generate hiprod= productivity_w+ invttail(n-1,0.025)*(sdprod  / sqrt(n))
*generate loprod= productivity_w  - invttail(n-1,0.025)*(sdprod  / sqrt(n))
gen barposition=0
replace barposition=1 if Post==0 & type==0
replace barposition=2 if Post==1 & type==0
replace barposition=4 if Post==0 & type==1
replace barposition=5 if Post==1 & type==1
replace barposition=7 if Post==0 & type==2
replace barposition=8 if Post==1 & type==2
twoway (bar bowls_peeled_w barposition if Post==0, sort barwidth(0.85) color(gray)) (rspike hibowls lobowls barposition if Post==0, sort lcolor(black) lwidth(medium)) (bar bowls_peeled_w barposition if Post==1, sort barwidth(0.85) color(navy)) (rspike hibowls lobowls barposition if Post==1, sort lcolor(black) lwidth(medium)),  plotregion(fcolor(white) lstyle(none) lcolor(white) lpattern(solid)) subtitle(, size(medlarge) nobox )  ylabel(1(1)7, nogrid labsize(med) angle(horizontal)) xlabel(1 "Pre" 2 "Post" 4 "Pre" 5 "Post" 7 "Pre" 8 "Post" , noticks labsize(med)) xscale(noline) graphregion(fcolor(white) lcolor(white) margin(tiny)) plotregion(fcolor(white)) ytitle("N. bowls peeled", size(medium)) xtitle("") legend(off) ysize(6) xsize(8) 
*twoway (bar peeling_time_w barposition if Post==0, sort barwidth(0.85) color(gray)) (rspike hitime lotime barposition if Post==0, sort lcolor(black) lwidth(medium)) (bar peeling_time_w barposition if Post==1, sort barwidth(0.85) color(navy)) (rspike hitime lotime barposition if Post==1, sort lcolor(black) lwidth(medium)),  plotregion(fcolor(white) lstyle(none) lcolor(white) lpattern(solid)) subtitle(, size(medlarge) nobox )  ylabel(1(1)8, nogrid labsize(med) angle(horizontal)) xlabel(1 "Pre" 2 "Post" 4 "Pre" 5 "Post" 7 "Pre" 8 "Post" , noticks labsize(med)) xscale(noline) graphregion(fcolor(white) lcolor(white) margin(tiny)) plotregion(fcolor(white)) ytitle("Peeling time", size(medium)) xtitle("") legend(off) ysize(6) xsize(8) 
*twoway (bar productivity_w barposition if Post==0, sort barwidth(0.85) color(gray)) (rspike hiprod loprod barposition if Post==0, sort lcolor(black) lwidth(medium)) (bar productivity_w barposition if Post==1, sort barwidth(0.85) color(navy)) (rspike hiprod loprod barposition if Post==1, sort lcolor(black) lwidth(medium)),  plotregion(fcolor(white) lstyle(none) lcolor(white) lpattern(solid)) subtitle(, size(medlarge) nobox )  ylabel(0(0.1)1, nogrid labsize(med) angle(horizontal)) xlabel(1 "Pre" 2 "Post" 4 "Pre" 5 "Post" 7 "Pre" 8 "Post" , noticks labsize(med)) xscale(noline) graphregion(fcolor(white) lcolor(white) margin(tiny)) plotregion(fcolor(white)) ytitle("Productivity", size(medium)) xtitle("") legend(off) ysize(6) xsize(8) 
restore


*goals by goal-setting types - Table A.9
preserve
keep if goals==1
tabulate type, generate(type_dummy)
reg Goals_peeled_bowls type_dummy2 type_dummy3 if week==4, cluster(employee)
test type_dummy2=type_dummy3
*outreg2 using "TableDiD_goals_bytype", tex replace addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
reg Goals_peeled_bowls type_dummy2 type_dummy3 if week==5, cluster(employee)
test type_dummy2=type_dummy3
*outreg2 using "TableDiD_goals_bytype", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
reg Goals_peeled_bowls type_dummy2 type_dummy3 if week==6, cluster(employee)
test type_dummy2=type_dummy3
*outreg2 using "TableDiD_goals_bytype", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
reg Goals_peeled_bowls type_dummy2 type_dummy3 if week==7, cluster(employee)
test type_dummy2=type_dummy3
*outreg2 using "TableDiD_goals_bytype", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
restore

*correlates of goal-setting types - Table A.10
preserve
keep if goals==1
ologit type  W1_age W1_gender W1_educ W1_hist_years  if n_employee==1
*outreg2 using "Table_types_obs", tex replace ctitle(Goal Type) addtext(Clustered S.E, Firm) label dec(3) pdec(3)
restore



**********************
*INTERVENTION EFFECTS*
**********************

*effect of goals on workers performance - Table 3
set more off
preserve
collapse (mean) bowls_peeled_w peeling_time_w productivity_w, by(date_peeled employee firm goals Post week) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD", tex replace addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg peeling_time_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
restore
*average product of labor
preserve
collapse tfp tfp_w, by(firm date_peeled goals week Post) 
xtset firm date_peeled
generate GoalsPost=goals*Post
xtreg tfp_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD", tex append addtext(Clustered S.E, Firm, Firm FE, YES, Time FE, YES) label
restore

*effect of goal setting by payment scheme - Table 4
*bowls peeled
set more off
preserve
*drop if payment method is unknown:
drop if W1_hist_pay_method=="5"
collapse (mean) bowls_peeled_w peeling_time_w productivity_w, by(date_peeled employee firm goals Post week piecerate) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled_w GoalsPost i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "TableDiD_payment", tex replace addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg bowls_peeled_w GoalsPost i.week if piecerate==0, fe cluster(firm) nonest
*outreg2 using "TableDiD_payment", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
*time spent peeling
*piecerate
xtreg peeling_time_w GoalsPost i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "TableDiD_payment", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
*flatrate
xtreg peeling_time_w GoalsPost i.week if piecerate==0, fe cluster(firm) nonest
*outreg2 using "TableDiD_payment", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
*productivity
xtreg productivity_w GoalsPost i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "TableDiD_payment", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w GoalsPost i.week if piecerate==0, fe cluster(firm) nonest
*outreg2 using "TableDiD_payment", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
restore

*ROBUSTENESS of the results
*winsor the dependent variable at 1%
winsor bowls_peeled, generate(bowls_peeled_1)  p(0.01)
winsor peeling_time, generate(peeling_time_1)  p(0.01)
winsor productivity, generate(productivity_1)  p(0.01)
winsor tfp, generate(tfp_1) p(0.01)

*effect of goals on workers performance, data winsorized at 1% and not winsorized- Table A.6
preserve
*winsorization 1%
set more off
collapse (mean) bowls_peeled_1 peeling_time_1 productivity_1, by(date_peeled employee firm goals Post week) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled_1 GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD_noWin", tex replace addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg peeling_time_1 GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD_noWin", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_1 GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD_noWin", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
restore
*non winsorized data
preserve
collapse (mean) bowls_peeled peeling_time productivity, by(date_peeled employee firm goals Post week) 
summarize bowls_peeled, detail
summarize peeling_time, detail
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD_noWin", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg peeling_time GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD_noWin", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD_noWin", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
restore

*average product of labor, winsor 1 and no winsor - not reported in paper
preserve
collapse tfp tfp_1, by(firm date_peeled goals week Post) 
xtset firm date_peeled
generate GoalsPost=goals*Post
xtreg tfp_1 GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "Tables/TableDiD_noWin", tex append addtext(Clustered S.E, Firm, Firm FE, YES, Time FE, YES) label
xtreg tfp GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "Tables/TableDiD_noWin", tex append addtext(Clustered S.E, Firm, Firm FE, YES, Time FE, YES) label
restore


*sub sample observed all periods - Table A.11 
*add dataset on employees presence in pre and post 
preserve
collapse (mean) bowls_peeled_w, by(employee firm Post goals)
gen temp=1 if Post==0 
replace temp=2 if Post==1
sort employee
by employee: egen presence = sum(temp)
collapse presence, by(employee goals)
replace presence=3 if presence>3
save "presence.dta", replace
restore
merge m:1 employee using "presence.dta"
tab _merge
drop  _merge
set more off
preserve
collapse (mean) bowls_peeled_w peeling_time_w productivity_w tfp_w presence, by(date_peeled employee firm goals Post week) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled_w GoalsPost i.week if presence==3, fe cluster(firm) nonest
*outreg2 using "Tables/TableDiD_alltime", tex replace addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg peeling_time_w GoalsPost i.week if presence==3, fe cluster(firm) nonest
*outreg2 using "Tables/TableDiD_alltime", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w GoalsPost i.week if presence==3, fe cluster(firm) nonest
*outreg2 using "Tables/TableDiD_alltime", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg tfp_w GoalsPost i.week if presence==3, fe cluster(firm) nonest
*outreg2 using "Tables/TableDiD_alltime", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
restore

*use a regression specification similar to ANCOVA - Table A.12
*open and run ancova.do file

*firms' hourly productivity as outcome- Table A.13
sort firm date_peeled
by firm date_peeled: egen sum_time=sum(peeling_time_w)
by firm date_peeled: generate mean_produ=n_bowls/sum_time
winsor mean_produ, generate(mean_produ_w) p(0.05)

preserve
collapse mean_produ_w piecerate, by(firm date_peeled goals week Post) 
xtset firm date_peeled
generate GoalsPost=goals*Post
xtreg  mean_produ_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD_mean_produ", tex replace addtext(Clustered S.E, Firm, Firm FE, YES, Time FE, YES) label
xtreg  mean_produ_w GoalsPost i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "TableDiD_mean_produ", tex append addtext(Clustered S.E, Firm, Firm FE, YES, Time FE, YES) label
restore


*Selection of workers into treatment, test main effects by number of employees - Table A.14
set more off
preserve
collapse (mean) bowls_peeled_w  peeling_time_w  productivity_w F1_staff_total, by(date_peeled employee firm goals Post week) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled_w GoalsPost i.week if F1_staff_total<5, fe cluster(firm) nonest
*outreg2 using "TableDiD_Nemployees", tex replace dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg bowls_peeled_w GoalsPost i.week if F1_staff_total>4, fe cluster(firm) nonest
*outreg2 using "TableDiD_Nemployees", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg peeling_time_w GoalsPost i.week if F1_staff_total<5, fe cluster(firm) nonest
*outreg2 using "TableDiD_Nemployees", tex append dec(3)addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg peeling_time_w GoalsPost i.week if F1_staff_total>4, fe cluster(firm) nonest
*outreg2 using "TableDiD_Nemployees", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w GoalsPost i.week if F1_staff_total<5, fe cluster(firm) nonest
*outreg2 using "TableDiD_Nemployees", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w GoalsPost i.week if F1_staff_total>4, fe cluster(firm) nonest
*outreg2 using "TableDiD_Nemployees", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
restore

*effect of goal setting on proxies of wellbeing - Table A.15
*treatment effect on life satisfaction  
generate W2_satis_life=W2_q9_1
reg W2_satis_life W1_satis_life t1 t2 if n_employee==1, cluster(firm)
test t1=t2
*outreg2 using "TableWelfare", tex replace dec(3) addtext(Clustered S.E, Firm) label
*consider payment scheme
reg  W2_satis_life W1_satis_life t1 t2 if n_employee==1 & piecerate==1, cluster(firm)
test t1=t2
*outreg2 using "TableWelfare", tex append dec(3) addtext(Clustered S.E, Firm) label
*by goal type
tabulate type, generate(goal_type)
reg  W2_satis_life W1_satis_life goal_type1 goal_type3 if n_employee==1 & goals==1, cluster(firm)
test goal_type1=goal_type3
*outreg2 using "TableWelfare", tex append dec(3) addtext(Clustered S.E, Firm) label
*inequality within the firm
sort firm date_peeled
by firm date_peeled: egen sd_bowls=sd(bowls_peeled)
preserve
collapse sd_bowls piecerate flatrate bowls_peeled, by(goals date_peeled firm Post week)
generate GoalsPost=goals*Post
xtset firm date_peeled
xtreg sd_bowls GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableWelfare", tex append dec(3) addtext(Clustered S.E, Firm) label
restore

*working days by goal-setting type - Table A.16
sort employee week
by employee week: egen days_work_week = count(date_peeled)
reg days_work_week i.goal_type if week==4 & goals==1
*outreg2 using "TableGoals_Dissatisf", tex replace dec(3) pdec(3)
reg days_work_week i.goal_type if week==5 & goals==1
*outreg2 using "TableGoals_Dissatisf", tex append dec(3) pdec(3)
reg days_work_week i.goal_type if week==6 & goals==1
*outreg2 using "TableGoals_Dissatisf", tex append dec(3) pdec(3)
reg days_work_week i.goal_type if week==7 & goals==1
*outreg2 using "TableGoals_Dissatisf", tex append dec(3) pdec(3)


************************
*HETEROGENEITY ANALYSIS*
************************

*PAYMENT SCHEME
*check whether firms adopt different schemes for different workers 
sort firm 
encode W1_hist_pay_method, generate(pay_scheme) 
by firm: egen sd_pay=sd(pay_scheme) if pay_scheme!=.
preserve 
collapse (max) sd_pay if pay_scheme!=., by(firm goals)
sort goals
by goals: tabulate sd_pay 
tabulate sd_pay 
restore 

*no treatment difference in payment methods:
tab goals piecerate if n_employee==1, chi2
tab goals flatrate if n_employee==1, chi2

*the treatment does not change firms' payment scheme - Table A.17
reg piecerate_post piecerate goals if n_employee==1, cluster(firm) 
*outreg2 using "Table_change_scheme", tex replace dec(3) addtext(Clustered S.E, Firm,) label
reg flatrate_post flatrate goals if n_employee==1, cluster(firm) 
*outreg2 using "Table_change_scheme", tex append dec(3) addtext(Clustered S.E, Firm,) label

*determinant of payment scheme adoption - Table A.18
reg piecerate goals F1_age F1_staff_total F1_staff_fam F1_firm_years_managing if n_firm==1
*outreg2 using "TableScheme_Firms", tex replace dec(3) addtext(Clustered S.E, Firm) label
reg flatrate goals F1_age F1_staff_total F1_staff_fam F1_firm_years_managing if n_firm==1
*outreg2 using "TableScheme_Firms", tex append dec(3) addtext(Clustered S.E, Firm) label


*CORRELATES OF SELF CONTROL:
*savings - Table A.19
set more off
preserve
collapse (mean) W1_house_savings bowls_peeled_w peeling_time_w productivity_w piecerate, by(date_peeled employee firm goals Post week) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled_w i.GoalsPost##c.W1_house_savings i.week, fe cluster(firm) nonest
*outreg2 using "savings", tex replace dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg bowls_peeled_w i.GoalsPost##c.W1_house_savings i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "savings", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg peeling_time_w i.GoalsPost##c.W1_house_savings i.week, fe cluster(firm) nonest
*outreg2 using "savings", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg peeling_time_w i.GoalsPost##c.W1_house_savings i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "savings", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) lab
xtreg productivity_w i.GoalsPost##c.W1_house_savings i.week, fe cluster(firm) nonest
*outreg2 using "savings", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w i.GoalsPost##c.W1_house_savings i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "savings", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
restore

*life satisfaction - Table A.20
set more off
preserve
collapse (mean) W1_satis_life bowls_peeled_w peeling_time_w productivity_w piecerate, by(date_peeled employee firm goals Post week) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled_w i.GoalsPost##c.W1_satis_life i.week, fe cluster(firm) nonest
*outreg2 using "life_satisf", tex replace dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg bowls_peeled_w i.GoalsPost##c.W1_satis_life i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "life_satisf", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg peeling_time_w i.GoalsPost##c.W1_satis_life i.week, fe cluster(firm) nonest
*outreg2 using "life_satisf", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg peeling_time_w i.GoalsPost##c.W1_satis_life i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "life_satisf", tex append dec(3)addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w i.GoalsPost##c.W1_satis_life i.week, fe cluster(firm) nonest
*outreg2 using "life_satisf", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w i.GoalsPost##c.W1_satis_life i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "life_satisf", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
restore

*impatience - Table A.21 
summarize W1_time_pref, detail
drop if W1_time_pref==1
generate super_impatient=1 if W1_time_pref>8 & W1_time_pref!=. 
replace super_impatient=0 if W1_time_pref<=8 & W1_time_pref!=. 
set more off
preserve
collapse (mean) W1_time_pref super_impatient bowls_peeled_w peeling_time_w productivity_w piecerate, by(date_peeled employee firm goals Post week) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled_w i.GoalsPost##i.super_impatient i.week, fe cluster(firm) nonest
*outreg2 using "impatience", tex replace dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg bowls_peeled_w i.GoalsPost##i.super_impatient i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "impatience", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg peeling_time_w i.GoalsPost##i.super_impatient i.week, fe cluster(firm) nonest
*outreg2 using "impatience", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg peeling_time_w i.GoalsPost##i.super_impatient i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "impatience", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w i.GoalsPost##i.super_impatient i.week, fe cluster(firm) nonest
*outreg2 using "impatience", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w i.GoalsPost##i.super_impatient i.week if piecerate==1, fe cluster(firm) nonest
*outreg2 using "impatience", tex append dec(3) addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
restore

*************************
*RANDOMIZATION INFERENCE*
*************************
*Table A.22
set more off
preserve
collapse (mean) bowls_peeled_w  peeling_time_w productivity_w piecerate, by(date_peeled employee firm goals Post week) 
xtset employee date_peeled

program define reg_bowls
xtreg bowls_peeled_w  goals##Post i.week, fe cluster(firm) nonest
*matrix list e(b)
end
ritest goals _b[1.goals#1.Post], reps(2000) cluster(firm): reg_bowls


program define reg_time
xtreg peeling_time_w  goals##Post i.week, fe cluster(firm) nonest
*matrix list e(b)
end
ritest goals _b[1.goals#1.Post], reps(2000) cluster(firm): reg_time

program define reg_productivity
xtreg productivity_w  goals##Post i.week, fe cluster(firm) nonest
*matrix list e(b)
end
ritest goals _b[1.goals#1.Post], reps(2000) cluster(firm): reg_productivity

program define reg_bowls_piecerate
xtreg bowls_peeled_w goals##Post i.week if piecerate==1, fe cluster(firm) nonest
*matrix list e(b)
end
ritest goals _b[1.goals#1.Post], reps(2000) cluster(firm): reg_bowls_piecerate

program define reg_time_piecerate
xtreg peeling_time_w  goals##Post i.week if piecerate==1, fe cluster(firm) nonest
*matrix list e(b)
end
ritest goals _b[1.goals#1.Post], reps(2000) cluster(firm): reg_time_piecerate

program define reg_productivity_piecerate
xtreg productivity_w  goals##Post i.week if piecerate==1, fe cluster(firm) nonest
*matrix list e(b)
end
ritest goals _b[1.goals#1.Post], reps(2000) cluster(firm): reg_productivity_piecerate
restore

******************************
*COMPETITION AND PEER EFFECTS*
******************************

*Peer effects - Table A.23
*Column 1: Do you prefer to peel more cassava than your colleagues in this firm, or the same amount of cassava that they peel?
generate peel_more=1 if W2_q9_12==1
replace peel_more=0 if missing(peel_more)
replace peel_more=. if missing(W2_q9_12)
preserve 
collapse (max) peel_more, by(firm employee goals treatment piecerate) 
tabulate treatment, generate(t)
reg peel_more t1 t2, cluster(firm)
test t1=t2
*outreg2 using "TableCompete", tex replace addtext(Clustered S.E, Firm) label
restore
*Column 2
*Goals: Setting goals for how much cassava I would peel each day made my job into more of a competition with my coworkers
*Production: Did you feel like the tracking of the production made your job into more of a competition?
generate compet=1 if W2_q9_23==5
replace compet=1 if W2_q9_23==4
replace compet=1 if  W2_q9_27==1
replace compet=0 if missing(compet)
replace compet=. if missing(W2_q9_23) & goals==1
replace compet=. if missing(W2_q9_27) & goals==0
preserve 
collapse (max) compet, by(firm employee goals treatment)
tabulate treatment, generate(t) 
reg compet goals, cluster(firm)
*outreg2 using "TableCompete", tex append addtext(Clustered S.E, Firm) label
restore

*effect of goal setting by competitive attitude - Table A.24
set more off
preserve
collapse (mean) bowls_peeled_w peeling_time_w productivity_w piecerate, by(peel_more date_peeled employee firm goals Post week) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled_w GoalsPost i.week if peel_more==0, fe cluster(firm) nonest
*outreg2 using "TableDiD_nocomp", tex replace addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg peeling_time_w GoalsPost i.week if peel_more==0, fe cluster(firm) nonest
*outreg2 using "TableDiD_nocomp", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg productivity_w GoalsPost i.week if peel_more==0, fe cluster(firm) nonest
*outreg2 using "TableDiD_nocomp", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg bowls_peeled_w GoalsPost i.week if peel_more==1, fe cluster(firm) nonest
*outreg2 using "TableDiD_nocomp", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg peeling_time_w GoalsPost i.week if peel_more==1, fe cluster(firm) nonest
*outreg2 using "TableDiD_nocomp", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg productivity_w GoalsPost i.week if peel_more==1, fe cluster(firm) nonest
*outreg2 using "TableDiD_nocomp", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
restore

*effect of goal setting on competitive attitudes - Table A.25
*check the within firm variability of workers composition
sort firm Post employee
by firm Post employee: egen N_worker=count(employee)
sort firm Post date_peeled
by firm Post date_peeled: generate N_day=_n==1
by firm Post: egen unique_date=sum(N_day)
by firm Post: egen max_obs=max(N_worker)
preserve
collapse N_worker max_obs unique_date, by(firm Post goals)
count if max_obs!=unique_date & goals==1 & Post==1
count if max_obs!=unique_date & goals==0 & Post==1
restore
preserve
set more off
collapse N_worker max_obs unique_date (max) peel_more F1_age F1_staff_total F1_staff_fam F1_firm_years_managing, by(firm employee Post goals treatment)
sort firm Post
by firm Post: egen var_workers=sd(N_worker)
replace var_workers=0 if missing(var_workers) & N_worker!=.
tab var_workers if goals==1
sum var_workers if goals==1 & var_workers>0
tab var_workers if goals==0
sum var_workers if goals==0 & var_workers>0
reg peel_more goals if var_workers==0, cluster(firm)
*outreg2 using "TableGoals_peers_stability", tex replace dec(3) pdec(3)
reg peel_more goals F1_age F1_staff_total F1_staff_fam F1_firm_years_managing if var_workers==0, cluster(firm)
*outreg2 using "TableGoals_peers_stability", tex append dec(3) pdec(3)
reg peel_more goals if var_workers>0, cluster(firm)
*outreg2 using "TableGoals_peers_stability", tex append dec(3) pdec(3)
reg peel_more goals F1_age F1_staff_total F1_staff_fam F1_firm_years_managing if var_workers>0, cluster(firm)
*outreg2 using "TableGoals_peers_stability", tex append dec(3) pdec(3)
restore

*effect of competitivenes on performance - Table A.26
preserve
keep if goals==1
collapse (mean)  bowls_peeled_w peeling_time_w productivity_w Goals_peeled_bowls (max) peel_more, by(date_peeled employee firm Post week)
xtset employee date_peeled
xtreg bowls_peeled_w i.peel_more i.week if Post==1, cluster(firm) nonest
*outreg2 using "TableDiD_competitiveW", tex replace addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg peeling_time_w i.peel_more i.week if Post==1, cluster(firm) nonest
*outreg2 using "TableDiD_competitiveW", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg productivity_w i.peel_more i.week if Post==1, cluster(firm) nonest
*outreg2 using "TableDiD_competitiveW", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg Goals_peeled_bowls i.peel_more i.week if Post==1, cluster(firm) nonest
*outreg2 using "TableDiD_competitiveW", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
restore 


*effect of goal setting by number of family members - Table A.27
set more off
tab F1_staff_fam if n_firm==1 & goals!=.
*max 2 family members
preserve
keep if F1_staff_fam<3
collapse (mean) bowls_peeled_w peeling_time_w productivity_w, by(date_peeled employee firm goals Post week) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD_family", tex replace addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg peeling_time_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD_family", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg productivity_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "TableDiD_family", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
restore
*more than 2 family members
preserve
keep if F1_staff_fam>2
collapse (mean) bowls_peeled_w peeling_time_w productivity_w, by(date_peeled employee firm goals Post week) 
xtset employee date_peeled
generate GoalsPost=goals*Post
xtreg bowls_peeled GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "Tables/TableDiD_family", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES) label
xtreg bowls_peeled_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "Tables/TableDiD_family_winsor", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg peeling_time_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "Tables/TableDiD_family_winsor", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
xtreg productivity_w GoalsPost i.week, fe cluster(firm) nonest
*outreg2 using "Tables/TableDiD_family_winsor", tex append addtext(Clustered S.E, Firm, Individual FE, YES, Time FE, YES, Winsorized, YES) label
restore

*time trends after goal setting training - Table A.28
preserve
collapse (mean) bowls_peeled_w peeling_time_w productivity_w, by(goals Post week firm employee date_peeled)
xtset employee date_peeled
xtreg bowls_peeled_w week if goals==1 & Post==1, fe robust cluster(firm) nonest
*outreg2 using "Tables/TableTrends", tex replace addtext(Clustered S.E, Firm, Firm FE, YES, Time FE, YES) label
xtreg peeling_time_w week if goals==1 & Post==1, fe robust cluster(firm) nonest
*outreg2 using "Tables/TableTrends", tex append addtext(Clustered S.E, Firm, Firm FE, YES, Time FE, YES) label
xtreg productivity_w week if goals==1 & Post==1, fe robust cluster(firm) nonest
*outreg2 using "Tables/TableTrends", tex append addtext(Clustered S.E, Firm, Firm FE, YES, Time FE, YES) label
restore

*treatment effects by week - Figure A.2
preserve
collapse (mean) bowls_peeled_w peeling_time_w productivity_w piecerate, by(employee firm goals Post week date_peeled) 
xtset employee date_peeled
gen g1w0 = (goals==1 & week==0)
gen g1w1 = (goals==1 & week==1)
gen g1w2 = (goals==1 & week==2)
gen g1w3 = (goals==1 & week==3)

gen g1w4 = (goals==1 & week==4)
gen g1w5 = (goals==1 & week==5)
gen g1w6 = (goals==1 & week==6)
gen g1w7 = (goals==1 & week==7)


xtreg bowls_peeled_w  bn.g1w3 g1w4 g1w5 g1w6 g1w7 if week<8 & week>2, fe cluster(firm) nonest
estimates store reg_bowls
coefplot reg_bowls, drop(_cons g) xline(0) ciopts(recast(rcap)) xlabel(4(1)7) yline(-1(.5)1, lpattern(dash) lcolor(gs12)) vertical ytitle("Coefficient Estimates", placement(e)) xtitle("Intervention Week")

xtreg productivity_w  bn.g1w3 g1w4 g1w5 g1w6 g1w7 if week<8 & week>2, fe cluster(firm) nonest
estimates store reg_bowls
coefplot reg_bowls, drop(_cons g) xline(0) ciopts(recast(rcap)) xlabel(4(1)7) yline(-1(.5)1, lpattern(dash) lcolor(gs12)) vertical ytitle("Coefficient Estimates", placement(e)) xtitle("Intervention Week")
restore

